package com.ocom.webapi.controller;

import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.payconfig.EntityMealMenu;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.MealPlanListRequest;
import com.ocom.common.request.payconfig.RemoveMealPlanRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.ImportMealMenuBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/2/22
 **/
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')")
@Slf4j
public class MealPlanController {

	@Autowired
	private PayConfigClientService payConfigClientService;

	@Autowired
	private ImportMealMenuBService excelParseService;


	@PostMapping(WebApiServiceUrl.Url_MealPlanOnShow)
	public Result MealPlanOnShow(@RequestBody MealPlanListRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
			request.setMerchId(yoCiUser.getMerchId().intValue());
		} else {
			request.setMerchId(0);
		}
		return payConfigClientService.mealPlanList(request);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanDelete)
	public Result MealPlanDelete(@RequestBody RemoveMealPlanRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
			request.setMerchId(yoCiUser.getMerchId().intValue());
		} else {
			request.setMerchId(0);
		}
		return payConfigClientService.removeMealPlan(request);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanAdd)
	public Result MealPlanAdd(@RequestBody EntityMealPlan entityMealPlan) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//			entityMealPlan.setMerchId(yoCiUser.getMerchId().intValue());
//		} else {
//			entityMealPlan.setMerchId(0);
//		}
		return payConfigClientService.newMealPlan(entityMealPlan);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanModify)
	public Result MealPlanModify(@RequestBody EntityMealPlan entityMealPlan) {
//		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//			entityMealPlan.setMerchId(yoCiUser.getMerchId().intValue());
//		} else {
//			entityMealPlan.setMerchId(0);
//		}
		return payConfigClientService.updateMealPlan(entityMealPlan);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanMenuList)
	public Result MealPlanMenuList(@RequestBody EntityMealMenu entityMealMenu) {
//		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//			entityMealMenu.setMerchId(yoCiUser.getMerchId().intValue());
//		} else {
//			entityMealMenu.setMerchId(0);
//		}
		return payConfigClientService.mealMenuList(entityMealMenu);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanClearMealMenu)
	public Result MealPlanClearMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
//		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//			entityMealMenu.setMerchId(yoCiUser.getMerchId().intValue());
//		} else {
//			entityMealMenu.setMerchId(0);
//		}
		return payConfigClientService.clearMealMenu(entityMealMenu);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanSetMealMenu)
	public Result MealPlanSetMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
//		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
//			entityMealMenu.setMerchId(yoCiUser.getMerchId().intValue());
//		} else {
//			entityMealMenu.setMerchId(0);
//		}
		return payConfigClientService.saveMealMenu(entityMealMenu);
	}

	@PostMapping(WebApiServiceUrl.Url_MealPlanDeleteMealMenu)
	public Result MealPlanDeleteMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if (yoCiUser.getRole() == 41L || yoCiUser.getRole() == 42L || yoCiUser.getMerchId()>0l) {
			entityMealMenu.setMerchId(yoCiUser.getMerchId().intValue());
		} else {
			entityMealMenu.setMerchId(0);
		}
		return payConfigClientService.removeMealMenu(entityMealMenu);
	}

	@GetMapping(WebApiServiceUrl.Url_MealPlanDownload)
	public Result MealPlanMealPlandownload() {
		return ResultUtil.success("/webapi/xiamenyiyuan_template.xlsx");
	}

	@GetMapping(WebApiServiceUrl.Url_MealPlanCDownload)
	public Result MealPlanMealCPlandownload() {
		return ResultUtil.success("/webapi/xiamenyiyuan_c_template.xlsx");
	}

	@PostMapping(value = WebApiServiceUrl.URL_MealPlanImportDataB, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public Result MealPlanImportDataB(Integer comId, MultipartFile file, String planName, Integer pointId, Integer dishesClassId,
																		String startDate, String stopDate, String remark,String tagId) {
		String filePath = file.getOriginalFilename();
		String filePaths = filePath.substring(filePath.length() - 4).toLowerCase();
		if (!filePaths.equals(".xls") & !filePaths.equals("xlsx")) {
			return ResultUtil.error(ResultEnum.Err_3058);
		}

		LocalDate startDate1 = null;
		LocalDate endDate1 = null;


		try {
			startDate1 = LocalDate.parse(startDate);
			endDate1 = LocalDate.parse(stopDate);
		} catch (Exception e) {
			return ResultUtil.error(ResultEnum.Err_2033);
		}

		DayOfWeek dayOfWeek = startDate1.getDayOfWeek();
		if (DayOfWeek.MONDAY != dayOfWeek) {
			return ResultUtil.error(ResultEnum.Err_2033);
		}

		if (excelParseService.checkPlanNameExist(comId, planName)){
			return ResultUtil.error(ResultEnum.Err_4120);
		}

		try {
			return excelParseService.importMealMenuB(comId, file, planName, pointId, dishesClassId,
					startDate1, endDate1, remark, tagId);
		} catch (Exception e) {
			
			log.info("导入菜品异常: 【{}】",e);
		
			return ResultUtil.error(ResultEnum.Err_900);
		}

	}


	@PostMapping(value = WebApiServiceUrl.URL_MealPlanImportDataC, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public Result MealPlanImportDataC(Integer comId, MultipartFile file, String planName, Integer pointId, Integer dishesClassId,
									  String startDate, String stopDate, String remark,String tagId) {
		String filePath = file.getOriginalFilename();
		String filePaths = filePath.substring(filePath.length() - 4).toLowerCase();
		if (!filePaths.equals(".xls") & !filePaths.equals("xlsx")) {
			return ResultUtil.error(ResultEnum.Err_3058);
		}

		LocalDate startDate1 = null;
		LocalDate endDate1 = null;


		try {
			startDate1 = LocalDate.parse(startDate);
			endDate1 = LocalDate.parse(stopDate);
		} catch (Exception e) {
			return ResultUtil.error(ResultEnum.Err_2033);
		}

		DayOfWeek dayOfWeek = startDate1.getDayOfWeek();
		if (DayOfWeek.MONDAY != dayOfWeek) {
			return ResultUtil.error(ResultEnum.Err_2033);
		}

		if (excelParseService.checkPlanNameExist(comId, planName)){
			return ResultUtil.error(ResultEnum.Err_4120);
		}

		try {
			return excelParseService.importMealMenuC(comId, file, planName, pointId, dishesClassId,
					startDate1, endDate1, remark, tagId);
		} catch (Exception e) {

			log.info("导入菜品异常: 【{}】",e);

			return ResultUtil.error(ResultEnum.Err_900);
		}

	}

	@GetMapping(WebApiServiceUrl.Url_MealMenuListB)
	public Result<Map<String, Object>> MealMenuListB(Long comId, Integer mealPlanId) {
		try {
			if (comId == null && mealPlanId == null) {
				return ResultUtil.error(ResultEnum.Err_203);
			}
			return payConfigClientService.mealMenuListB(comId, mealPlanId);
		} catch (Exception e) {
		
			log.info("获取菜品异常: 【{}】",e);
		
			return ResultUtil.error(ResultEnum.Err_900);
		}
	}


}
